//
//  CustomTabBarController.m
//  ShopXinyi
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "CustomTabBarController.h"
#import "MainViewController.h"
#import "CategoryViewController.h"
#import "DiscountViewController.h"
#import "CartViewController.h"
#import "MemberViewController.h"
#import "FMDBManager.h"
#import "UserInfoModel.h"
#import "HttpRequestManager.h"

@interface CustomTabBarController ()
{
    UIImageView *customBar;
    UIView *_svBgrdView;
}
@end

@implementation CustomTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    customBar = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-45, self.view.frame.size.width, 45)];
    
    [self loadFirstLanchView];
    
    customBar.userInteractionEnabled = YES;
    customBar.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1.0];
    
    [self.view addSubview:customBar];
    
    NSArray *normalImg = @[@"class3@2x",@"listImg@2x",@"goods_sort_arr",@"index_04@2x",@"index_05"];
    NSArray *selectImg = @[@"member_dsh",@"choseTime",@"goods_sort_arr_red",@"goods_buy",@"index_05"];
    NSArray *nameArray = @[@"首页",@"分类",@"天天特价",@"购物车",@"会员中心"];
 
    
    
    for (int i = 0; i < 5; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * self.view.frame.size.width/5, 0, self.view.frame.size.width/5, 35);
        [button setImage:[UIImage imageNamed:normalImg[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:selectImg[i]] forState:UIControlStateSelected];
        [button imageRectForContentRect:CGRectMake(0, 0, 30, 30)];

        button.tag = 100 + i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [customBar addSubview:button];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(i*self.view.frame.size.width/5, 30, self.view.frame.size.width/5, 15)];
        label.text = nameArray[i];
        label.font = [UIFont systemFontOfSize:11];
        label.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1];
        label.textAlignment = NSTextAlignmentCenter;
        label.tag = 200+i;
        [customBar addSubview:label];
        
    }
    
  
    UINavigationController *nvc = [UINavigationController new];
    UIStoryboard *mainstory = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    nvc = [mainstory instantiateViewControllerWithIdentifier:@"mainNVC"];
    
    UINavigationController *classNvc = [UINavigationController new];
    classNvc = [mainstory instantiateViewControllerWithIdentifier:@"classNVC"];
    
    DiscountViewController *vc3 = [DiscountViewController new];
    vc3 = [mainstory instantiateViewControllerWithIdentifier:@"discVC"];
    
    CartViewController *vc4 = [CartViewController new];
    vc4 = [mainstory instantiateViewControllerWithIdentifier:@"cartVC"];
    
    MemberViewController *vc5 = [MemberViewController new];
    vc5 = [mainstory instantiateViewControllerWithIdentifier:@"membVC"];
    self.viewControllers = @[nvc,classNvc,vc3,vc4,vc5];
    
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    UILabel *label = (UILabel *)[self.view viewWithTag:200];
    label.textColor = [UIColor colorWithRed:225/255.0 green:32/255.0 blue:48/255.0 alpha:0.9];
    [button setSelected:YES];
    
    [self loadDataFromDB];
    
}




#pragma mark -FMDB 数据库
-(void)loadDataFromDB
{
    FMDBManager *manager = [FMDBManager shareInstance];
    UserInfoModel *userInfo = [UserInfoModel shareInstance];
    FMResultSet *rs = [manager.dataBase executeQuery:@"select * from User"];
    while ([rs next]) {
        userInfo.userName = [rs stringForColumn:@"username"];
        userInfo.password = [rs stringForColumn:@"password"];
        userInfo.client = [rs stringForColumn:@"client"];
    }
    
    [[HttpRequestManager shareInstance] loginWithClient:userInfo.client andPassword:userInfo.password andUsername:userInfo.userName sueecss:^(id responseObj) {
        
        //解析用户数据
        NSDictionary *dic = responseObj;
        NSString *keyStr = dic[@"key"];
        userInfo.key = keyStr;
        //加入数据库
        //1.清除数据库
        FMDBManager *manager = [FMDBManager shareInstance];
        if (![manager.dataBase executeUpdate:@"delete from User"]) {
            NSLog(@"清除表 User 失败");
        }
        //2.插入数据
        if (![manager.dataBase executeUpdate:@"insert into User(client, password , username, key) values('ios',?,?,?)",userInfo.password,userInfo.userName,keyStr]) {
            NSLog(@"插入用户数据失败");
        }
    } failure:^(NSError *error) {
        NSLog(@"获取 key 失败,error==%@",error);
    }];
}

-(void)buttonClick:(UIButton *)button
{
    UserInfoModel *userInfo = [UserInfoModel shareInstance];
    if (button.tag == 104) {
        if (userInfo.userName.length > 0) {
        }else{
            [self performSegueWithIdentifier:@"toLogin" sender:self];
        return;
        }
    }
    
    for (int i = 0; i < 5; i++) {

        UIButton *button = (UIButton *)[self.view viewWithTag:100+i];
        button.selected = NO;
        
        UILabel *label = (UILabel *)[self.view viewWithTag:200+i];
        label.textColor = [UIColor colorWithRed:0.5 green:0.5 blue:0.5 alpha:1.0];
    }
    button.selected = YES;
    self.selectedIndex = button.tag - 100;
    UILabel *label = (UILabel *)[self.view viewWithTag:200+self.selectedIndex];
    label.textColor = [UIColor colorWithRed:225/255.0 green:32/255.0 blue:48/255.0 alpha:0.9];
}



// 进入按钮点击事件
- (void)enterHomeVC
{
    [UIView animateWithDuration:1.5 animations:^{
        _svBgrdView.alpha = 0;
    } completion:^(BOOL finished) {
        [_svBgrdView removeFromSuperview];
        customBar.hidden = NO;
    }];
}

- (void)loadFirstLanchView
{
//     判断沙盒中是否有firstLanch的值
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLanch"])
    {
        return;
    }
    customBar.hidden = YES;

    _svBgrdView = [[UIView alloc] initWithFrame:self.view.bounds];
    
    UIScrollView *_sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    _sv.contentSize = CGSizeMake(_sv.bounds.size.width * 4, _sv.bounds.size.height);
    _sv.bounces = NO;
    _sv.pagingEnabled = YES;
    [_svBgrdView addSubview:_sv];
    
    for (int i = 0; i < 4; i++)
    {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_sv.bounds.size.width * i, 0, _sv.bounds.size.width, _sv.bounds.size.height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"guid_0%d", i + 1]];
        [_sv addSubview:imageView];
    }
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(_sv.bounds.size.width * 3.5 - 50, _sv.bounds.size.height - 100, 100, 30);
    [btn setImage:[UIImage imageNamed:@"guide_btn"] forState:UIControlStateNormal];
    [btn addTarget:self  action:@selector(enterHomeVC) forControlEvents:UIControlEventTouchUpInside];
    [_sv addSubview:btn];
    
    // 保存key为firstLanch的值为YES
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLanch"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}




@end
